package br.com.educa.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

import org.springframework.security.authentication.encoding.MessageDigestPasswordEncoder;

public class EncodeJsUtil {
	
	public String encode(String path){
		if(path.contains("EducaJ/src/main/webapp/educa")){
			path = path.replace("EducaJ/src/main/webapp/educa","workspace_mj/sigen/src/main/webapp");
		}

		Scanner scannerLinhas=null;
		String table =null;
		String where =null;
		String classe =null;
		try{
			File js = new File(path);
			FileReader jsReader = new FileReader(js);
			scannerLinhas = new Scanner(jsReader);
			while ( scannerLinhas.hasNextLine() ){
		        String linha =  scannerLinhas.nextLine();
		        if(linha.contains("var table")){
		        		table = linha.substring(linha.indexOf("\"")+1, linha.lastIndexOf("\""));
		        }else if(linha.contains("var where")){
		        		where = linha.substring(linha.indexOf("\"")+1, linha.lastIndexOf("\""));
		        }else if(linha.contains("var classe")){
		        		classe = linha.substring(linha.indexOf("\"")+1, linha.lastIndexOf("\""));
		        }
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			scannerLinhas.close();
		}
		MessageDigestPasswordEncoder passwordEncoder = new MessageDigestPasswordEncoder("MD5");
//		System.out.println(table +"-> "+ passwordEncoder.encodePassword(table, "cdfrpd09_*m9s2h3u7O"));
//		System.out.println(where+ "-> "+ passwordEncoder.encodePassword(where, "cdfrpd09_*m9s2h3u7O"));
//		System.out.println(classe+ "-> "+ passwordEncoder.encodePassword(classe, "cdfrpd09_*m9s2h3u7O"));
        String encoded = passwordEncoder.encodePassword((table+where+classe), "cdfrpd09_*m9s2h3u7O");
//        System.out.print("\nJsp:"+encoded+ "-> Controller: ");
		return encoded;
	}

  public static void main(String args[]) throws FileNotFoundException {
	  EncodeJsUtil parser = new EncodeJsUtil();
	  parser.encode("/home/Ataides/workspace/EducaJ/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/educa/WEB-INF/screen/evento/listarEvento.js");
  }
  
} 
